package com.alogic.together2.service;

import com.alogic.together2.TogetherConstants;
import com.logicbus.backend.message.YamlMessage;
import org.apache.commons.lang3.StringUtils;

import com.alogic.together.service.SevantLogicletContext;
import com.alogic.together2.TogetherServiceDescription;
import com.alogic.xscript.LogicletContext;
import com.alogic.xscript.Script;
import com.alogic.xscript.doc.XsObject;
import com.alogic.xscript.doc.json.JsonObject;
import com.alogic.xscript.doc.xml.XmlObject;
import com.anysoft.util.Properties;
import com.anysoft.util.PropertiesConstants;
import com.logicbus.backend.AbstractServant;
import com.logicbus.backend.Context;
import com.logicbus.backend.message.JsonMessage;
import com.logicbus.backend.message.XMLMessage;
import com.logicbus.models.servant.ServiceDescription;

/**
 * 专用Servant
 * @author yyduan
 * 
 * @since 1.6.11.3
 * 
 * @version 1.6.11.22 [duanyy 20180314] <br>
 * - 支持按指定jsonpath路径来输出文档 <br>
 * 
 * @version 1.6.11.59 [20180911 duanyy] <br>
 * - 使用新的ServantLogicletContext类;
 *
 * @version 1.6.12.27 [20190403 duanyy] <br>
 * - 增加对yaml的支持 <br>
 *
 * @version 1.6.12.39 [20190725] <br>
 * - 增加$privilege上下文变量 <br>
 *
 * @version 1.6.14.1 [20210310 duanyy] <br>
 * - 支持$status脚本变量作为返回status值 <br>
 *
 * @version 1.6.14.5 [20210413 duanyy] <br>
 * - 字符串常量使用静态变量 <br>
 */
public class TogetherServant extends AbstractServant implements TogetherConstants {
	protected Script script = null;
	protected String service;
	protected String privilege;
	
	@Override
	protected void onDestroy() {
		
	}

	@Override
	protected void onCreate(ServiceDescription sd) {
		service = sd.getPath();
		privilege = sd.getPrivilege();

		if (sd instanceof TogetherServiceDescription){
			script = ((TogetherServiceDescription)sd).getScript();
		}else{
			Properties p = sd.getProperties();
			String bootstrap = PropertiesConstants.getString(p,BOOTSTRAP,EMPTY,true);
			if (StringUtils.isEmpty(bootstrap)){
				String config = PropertiesConstants.getString(p,SCRIPT,EMPTY);
				if (StringUtils.isNotEmpty(config)){
					script = Script.create(config, p);
				}
			}else{
				String config = PropertiesConstants.getString(p,SCRIPT,EMPTY);
				if (StringUtils.isNotEmpty(config)){
					script = Script.create(bootstrap, config, p);
				}
			}
		}
	}

	@Override
	protected int onJson(Context ctx)  {
		if (script != null){
			JsonMessage msg = (JsonMessage) ctx.asMessage(JsonMessage.class);
			
			LogicletContext logicletContext = new Context.ServantLogicletContext(ctx);
			logicletContext.setObject(ID_CONTEXT, ctx);
			logicletContext.SetValue(ID_SERVICE, service);
			logicletContext.SetValue(ID_PRIVILEGE,StringUtils.isEmpty(privilege)?service:privilege);
			try {
				XsObject doc = new JsonObject(ROOT,msg.getRoot());
				script.execute(doc,doc, logicletContext, null);
			}finally{
				logicletContext.removeObject(ID_CONTEXT);
				
				String keyword = logicletContext.GetValue(ID_KEYWORD, EMPTY);
				if (StringUtils.isNotEmpty(keyword)){
					ctx.setKeyword(keyword);
				}			
				
				String outputPath = logicletContext.GetValue(ID_OUTPATH, EMPTY);
				if (StringUtils.isNotEmpty(outputPath)){
					msg.setOutputPath(outputPath);
				}

				ctx.setStatus(PropertiesConstants.getInt(logicletContext,ID_STATUS,Context.DFT_STATUS));
			}
		}else{
			ctx.asMessage(JsonMessage.class);
		}
		return 0;
	}

	@Override
	protected int onYaml(Context ctx)  {
		if (script != null){
			YamlMessage msg = (YamlMessage) ctx.asMessage(YamlMessage.class);

			LogicletContext logicletContext = new Context.ServantLogicletContext(ctx);
			logicletContext.setObject(ID_CONTEXT, ctx);
			logicletContext.SetValue(ID_SERVICE, service);
			logicletContext.SetValue(ID_PRIVILEGE,StringUtils.isEmpty(privilege)?service:privilege);
			try {
				XsObject doc = new JsonObject(ROOT,msg.getRoot());
				script.execute(doc,doc, logicletContext, null);
			}finally{
				logicletContext.removeObject(ID_CONTEXT);

				String keyword = logicletContext.GetValue(ID_KEYWORD, EMPTY);
				if (StringUtils.isNotEmpty(keyword)){
					ctx.setKeyword(keyword);
				}

				String outputPath = logicletContext.GetValue(ID_OUTPATH, EMPTY);
				if (StringUtils.isNotEmpty(outputPath)){
					msg.setOutputPath(outputPath);
				}

				ctx.setStatus(PropertiesConstants.getInt(logicletContext,ID_STATUS,Context.DFT_STATUS));
			}
		}else{
			ctx.asMessage(YamlMessage.class);
		}
		return 0;
	}

	protected int onXml(Context ctx) { 
		if (script != null){
			XMLMessage msg = (XMLMessage) ctx.asMessage(XMLMessage.class);
			
			LogicletContext logicletContext = new SevantLogicletContext(ctx);
			logicletContext.setObject(ID_CONTEXT, ctx);
			logicletContext.SetValue(ID_SERVICE, service);
			logicletContext.SetValue(ID_PRIVILEGE,StringUtils.isEmpty(privilege)?service:privilege);
			try {
				XsObject doc = new XmlObject(ROOT,msg.getRoot());
				script.execute(doc,doc, logicletContext, null);
			}finally{
				logicletContext.removeObject(ID_CONTEXT);
				
				String keyword = logicletContext.GetValue(ID_KEYWORD, EMPTY);
				if (StringUtils.isNotEmpty(keyword)){
					ctx.setKeyword(keyword);
				}

				ctx.setStatus(PropertiesConstants.getInt(logicletContext,ID_STATUS,Context.DFT_STATUS));
			}
		}else{
			ctx.asMessage(XMLMessage.class);
		}
		return 0;	
	}
}